<?php
class Controller_Online_EbayProduct extends Controller_Abstract
{
	function actionImport()
	{
		$this->_view['title'] = '导入信息';
	}
	
	function actionImportSave()
	{
		$this->_view['title'] = '导入信息（保存）';
		if ($this->_context->isPOST())
		{
			$upload_result = Upload_Files::uploadFile('product_info', Q::ini('custom_flag/upload_file_type/online_ebay_product/value'));
			if ($upload_result['ack'] == SUCCESS)
			{
				$product_info = Helper_BSS_XLS::xlsFileToArray($upload_result['path'], $upload_result['file_id']);
				$result = Online_Ebay_Product::import($product_info);
				if ($result['ack'] == SUCCESS)
				{
					return $this->_redirectMessage('导入成功', '正在返回..', url('Online::EbayProduct/List'), $result['ack']);
				}
				else
				{
					return $this->_redirectMessage('部分导入失败', $result['message'], url('Online::EbayProduct/List'), $result['ack']);
				}
			}
			else
			{
				return $this->_redirectMessage('文件导入失败', '正在返回..', $_SERVER['HTTP_REFERER'], $upload_result['ack']);
			}
		}
	}
	
	function actionList()
	{
		$this->_view['title'] = '信息列表';
		$file = Upload_Files::find('type=?', Q::ini('custom_flag/upload_file_type/online_ebay_product/value'))->order('time desc')->asArray()->getAll();
		$this->_view['file'] = $file;
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 10;
		$product_categories = Product_Categories::getCategoriesTree();
		$this->_view['product_categories'] = json_encode($product_categories);
		$url_arr = Helper_BSS_Normal::buildCondition();
		$this->_view['url_arr'] = json_encode($url_arr);
		$result = Online_Ebay_Product::search($url_arr, $cp, $ps);
		if ($result['ack'] == SUCCESS)
		{
			$product_info = $result['data'];
			$page = new Helper_BSS_Page($result['pagination'], url('Online::EbayProduct/List', $url_arr));
			$this->_view['page'] = $page->getPage();
			Helper_BSS_Normal::getProductImgUrl($product_info, 'number');
			$this->_view['product_info'] = $product_info;
		}
	}
	
	function actionEdit()
	{
		$this->_view['title'] = '信息编辑';
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		if ($id)
		{
			$product_info = Online_Ebay_Product::find('online_ebay_product.id=?', $id)
				->joinleft('product_basic', 'product_basic.number, product_basic.name', 'product_basic.id=online_ebay_product.product_basic_id')
				->asArray()->getAll();
			Helper_BSS_Normal::getProductImgUrl($product_info, 'number');
			$this->_view['product_info'] = $product_info[0];
		}
	}
	
	function actionEditSave()
	{
		$this->_view['title'] = '信息编辑（保存）';
		if ($_POST['id'])
		{
			$product_info = Online_Ebay_Product::find('id=?', $_POST['id'])->getOne();
			if ($product_info)
			{
				$result = Online_Ebay_Product::editSave($product_info);
				if ($result['ack'] == SUCCESS)
				{
					return $this->_redirectMessage('保存成功', '正在返回..', url('Online::EbayProduct/List'), $result['ack']);
				}
			}
			else
			{
				return $this->_redirectMessage('保存失败', '没有确定产品id', url('Online::EbayProduct/List'), $result['ack']);
			}
		}
	}
	
	function actionExportCheck()
	{
		$this->_view['title'] = '导出（按勾选）';
		$ids = $_POST['ids'];
		$product_info = Online_Ebay_Product::find('online_ebay_product.id in (' . $ids . ')')
			->joinleft('product_basic', 'product_basic.number, product_basic.name', 'product_basic.id=online_ebay_product.product_basic_id')
			->asArray()->getAll();
		Helper_BSS_XLS::ebayProductInfoExport($product_info);
	}
	
	function actionExportSearch()
	{
		$this->_view['title'] = '导出（按搜索）';
		$temp = json_decode($_POST['url_arr']);
		$url_arr = array();
		foreach ($temp as $key => $t)
		{
			$url_arr[$key] = $t;
		}
		$where = Online_Ebay_Product::buildSearchWhere($url_arr);
		$product_info = Online_Ebay_Product::find($where)
			->joinleft('product_basic', 'product_basic.number, product_basic.name', 'product_basic.id = online_ebay_product.product_basic_id')
			->asArray()->getAll();
		Helper_BSS_XLS::ebayProductInfoExport($product_info);
	}
}